package repository

import (
	"gorm.io/gorm"
	"shop-common/utils/pagehelper"
	"shop-common/utils/queryWrapper"
	"shop-sys/model"
)

type ISysLogRepository interface {
	Create(*gorm.DB, *model.SysLog) (uint, error)
	FindPage(*gorm.DB, *pagehelper.Params) ([]*model.SysLog, error)
	Count(*gorm.DB, *pagehelper.Params) (int64, error)
}

type SysLogRepository struct{}

func NewSysLogRepository() ISysLogRepository {
	return &SysLogRepository{}
}

func (r *SysLogRepository) Create(db *gorm.DB, history *model.SysLog) (uint, error) {
	return history.Id, db.Create(&history).Error
}

func (r *SysLogRepository) FindPage(db *gorm.DB, params *pagehelper.Params) (list []*model.SysLog, err error) {
	tx := queryWrapper.ReuseWherePair(db.Model(&model.SysLog{}), params.WherePair)
	return list, tx.Scopes(pagehelper.Paginate(params.Current, params.Size)).
		Order("created_at desc").Find(&list).Error
}

func (r *SysLogRepository) Count(db *gorm.DB, params *pagehelper.Params) (count int64, err error) {
	tx := queryWrapper.ReuseSelect(db.Model(&model.SysLog{}), params.SelectFields)
	return count, queryWrapper.ReuseWherePair(tx, params.WherePair).Count(&count).Error
}
